#include "msp430g2152.h" // Include the necessary header for our target MCU
#define CLOCK_ON P1OUT |= BIT5
#define DATA_ON P2OUT |= BIT0 
#define BUSY_ON P2OUT |= BIT1
#define RESET_ON P2OUT |= BIT2 

#define CLOCK_OFF P1OUT &= ~BIT5
#define DATA_OFF P2OUT &= ~BIT0 
#define BUSY_OFF P2OUT &= ~BIT1
#define RESET_OFF P2OUT &= ~BIT2 

void delay_ms(unsigned int count)
{
	unsigned int i,j;
	for(i=0;i<count;i++)
	{ 
	   _delay_cycles(16000-1);
	}	
}

void delay_us(unsigned int n)
{
unsigned int i;
	for(i=0;i<n;i++)
	{ 
	  _delay_cycles(15);
	}
}
#define SELECT 0

const unsigned int Stop= 0xFFFF; // Stop
long int Song; // current Song
unsigned int vol; // current volume

/**********************************************************************************
Send Sequence
**********************************************************************************/

void sendData(long int TheSong)
{
long int ClockCounter=0;
int ClockCycle=15;//0x0f;

CLOCK_ON; // Hold (idle) for 300msec to prepare data start
delay_ms(300);
CLOCK_OFF; //Hold for 2msec to signal data start
delay_ms(2); //2msec delay

while(ClockCounter <= ClockCycle)
{
CLOCK_OFF;;

if (TheSong & 0x8000)
{
CLOCK_ON;
}
else
{
DATA_ON;
}

TheSong = TheSong << 1;
delay_us(200); //Clock cycle period is 200 uSec - LOW
CLOCK_ON;
ClockCounter++;
delay_us(200); //Clock cycle period is 200 uSec - HIGH
}
DATA_OFF;
CLOCK_ON; // Place clock high to signal end of data
}

/**********************************************************************************
Plays Stored Song by Number
**********************************************************************************/

void PlaySong(long int SongNumber)
{
sendData(Stop);
sendData(SongNumber);
}

/**********************************************************************************
Reset SOMO
**********************************************************************************/
void Reset()
{
RESET_OFF;
delay_ms(50);
RESET_ON;
Song=0;
}

void main()
{
int cmd;
DCOCTL = 0;                             // Run at 16 MHz
    BCSCTL1 = CALBC1_16MHZ;                 //
    DCOCTL = CALDCO_16MHZ;     
	P2DIR |= BIT0|BIT2;
	P1DIR |= BIT5;    
	 
	P2REN |= BIT0|BIT2;
	P1REN |= BIT5;
Reset();
delay_ms(200);
PlaySong(004); //"sx"; x=Play Song Number
delay_ms(2000);
while(1);/*
{
PlaySong(4); //"sx"; x=Play Song Number
delay_ms(2000);
PlaySong(2); //"sx"; x=Play Song Number
delay_ms(2000);
PlaySong(3); //"sx"; x=Play Song Number
delay_ms(2000);
while(9);	
/*if(RF_A==SELECT)
{
PlaySong(4); //"sx"; x=Play Song Number
delay_ms(800); // Pause 2 seconds to show User selection
}
else if(RF_B==SELECT)
{
PlaySong(5); //"sx"; x=Play Song Number
delay_ms(800); // Pause 2 seconds to show User selection
}
else if(RF_C==SELECT)
{
PlaySong(1); //"sx"; x=Play Song Number
delay_ms(800); // Pause 2 seconds to show User selection
}
else if(RF_D==SELECT)
{
PlaySong(2); //"sx"; x=Play Song Number
delay_ms(800); // Pause 2 seconds to show User selection
}*/
//}
}
